/*
 * ForkThread.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Power(power@kermit.cs.pub.ro)
 * 
 * Obs:
 */


package engine;
import java.util.*;

/**
 * A newly fork'ed thread
 */

class ForkThread extends ControlThread{
	Fork launcher;

	ForkThread(Fork launcher, Instruction rocket) {
		super(rocket);
		this.launcher = launcher;
	}

	public void run() {
		try{
			launcher.anyThreadStarted = true;
			launcher.count++;
			try{
				while(ins != null) {
					synchronized(xConsole.lock) {
						if (!xConsole.enableExecution()) {
							Time.stopSimulation();
							throw new SimulationStoppedException();
						}
						xConsole.consumeStep();
					}            
					ins.execute();
					ins = ins.next();
					xConsole.debug("coming: " + ins);
				}
				xConsole.debug("Thread " + this + " done.");
			}catch(InterpretTimeException ex){
				xConsole.dumpStack(ex);
				xConsole.cout(ex.toString());
				Time.stopSimulation();
			}
			Time.removeThread(this);
			launcher.count--;
			launcher.wakeUp();
		}catch(SimulationStoppedException ex){
			xConsole.debug(ex + " " + this);
		}
	}
}
